package com.llmcu.source.java8.java8inaction.chapter12;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

/**
 * TemporalAdjuster使用示例
 *
 * @author liuling
 * @date 2024-12-18 16:06
 */
public class TemporalAdjusterDemo {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
        // 下一个或当前的星期几
        LocalDateTime nextOrSameWednesday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
        LocalDateTime nextWednesday = now.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
        LocalDateTime previousOrSameTuesday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
        LocalDateTime previousTuesday = now.with(TemporalAdjusters.previous(DayOfWeek.TUESDAY));
        // 这个月第几个星期几，或倒数第几个星期几（如果是负数的话）。（所以取值范围是-5到5）
        LocalDateTime sundayInMonth = now.with(TemporalAdjusters.dayOfWeekInMonth(-1, DayOfWeek.SUNDAY));
        // first系列
        LocalDateTime firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
        // last系列
        LocalDateTime lastDayOfYear = now.with(TemporalAdjusters.lastDayOfYear());

        System.out.println("lastDayOfMonth:" + lastDayOfMonth);
        System.out.println("nextOrSameWednesday:" + nextOrSameWednesday);
        System.out.println("nextWednesday:" + nextWednesday);
        System.out.println("previousOrSameTuesday:" + previousOrSameTuesday);
        System.out.println("previousTuesday:" + previousTuesday);
        System.out.println("sundayInMonth:" + sundayInMonth);
        System.out.println("firstDayOfMonth:" + firstDayOfMonth);
        System.out.println("lastDayOfYear:" + lastDayOfYear);
        System.out.println("now:" + now);
    }
}
